﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test
{
    /// <summary>
    /// 出于性能考虑，计算Offer评分分级，不使用配置，表达示引擎求值 。索引时，Offer质量评分仍然使用。配置
    /// </summary>
    public class ScoreUtils
    {
        public static int SCORE_BITS = 8;

        public int m_digitsOrder = 8;//评分位数  8 千万位

        public int m_relevanceLevel = 9;//相关度分多少级
        /// <summary>
        /// 保存分级的上边界.下标为 分级.值为上边界  levelMapper[0] 不用。
        /// </summary>
        private double[] levelMapper = null;

        public ScoreUtils()
        {
            levelMapper = new double[m_relevanceLevel + 1];//levelMapper[0] 不用
            levelMapper[9] = 4;
            levelMapper[8] = 3;
            levelMapper[7] = 2;
            levelMapper[6] = 1;
            levelMapper[5] = 0.9;
            levelMapper[4] = 0.7;
            levelMapper[3] = 0.5;
            levelMapper[2] = 0.3;
            levelMapper[1] = 0.1;
            //levelMapper[0] = 0;
        }

        private Dictionary<double, double> cache = new Dictionary<double, double>();
        public double GetRelevanceScore(double score)
        {
            //if (cache.ContainsKey(score))
            //    return cache[score];

            for (int i = m_relevanceLevel; i > 0; i--)
            {
                if (score >= levelMapper[i])
                {
                    double ret= i * Math.Pow(10, m_digitsOrder) + score;
                    //cache.Add(score, ret);
                    return ret;
                }
            }
            //cache.Add(score, 0);
            return 0;
        }

        public double GetRelevanceScoreCache(double score)
        {
            if (cache.ContainsKey(score))
                return cache[score];

            for (int i = m_relevanceLevel; i > 0; i--)
            {
                if (score >= levelMapper[i])
                {
                    double ret = i * Math.Pow(10, m_digitsOrder) + score;
                    cache.Add(score, ret);
                    return ret;
                }
            }
            cache.Add(score, 0);
            return 0;
        }
        
        public int GetRelevanceLevel(double score)
        {
            for (int i = m_relevanceLevel; i > 0; i--)
            {
                if (score > levelMapper[i])
                {
                    return i;
                }
            }
            return 0;
        }
    }
}
